package com.cs.pk.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public final class UtilRepository {
	
	private static final HashMap viewActivity = new HashMap();
	private static final HashMap viewDirectory = new HashMap();
	private static final HashMap obsoleteItems = new HashMap();
	private static final HashMap projectItems = new HashMap();
	private static final HashMap streamItems = new HashMap();
	
	public final static int NO_REPOSITORY = -1;
	public final static int VIEW_REPOSITORY = 0;
	public final static int ACTIVITY_REPOSITORY = 1;
	public final static int OBSOLETE_REPOSITORY = 2 ;
	public final static int PROJECT_REPOSITORY = 3;
	public final static int STREAM_REPOSITORY = 4;
	public static String DRIVE = null; 
	
	
	/**
	 * a view belongs to multiple activities.
	 * So activity should be a key and view as a value. 
	 * 
	 * 
	 * @param activity
	 * @param view
	 */
	public synchronized static void updateViewActivityRepository(Object activity, Object view) {
		viewActivity.put(activity, view);
	}
	
	/**
	 * Different methods for updation of repository required as to make it thread safe and fast at the same time 
	 * 
	 * Putting un-necessary check but some time this behaviour has been noticed.
	 * @param view
	 * @param dir
	 */
	public synchronized static void updateViewDirectoryRepository(Object view, Object dir) {
		if(viewDirectory.containsKey(view)) {
			if(!dir.toString().endsWith("@@")) {
		       viewDirectory.put(view, dir);
			}
		}else {
			 viewDirectory.put(view, dir);
		}
	}
	
	public synchronized static void updateObsoleteRepository(Object text,Object id) {
		obsoleteItems.put(text, id);
	}
	
	public synchronized static void updateProjectRepository(Object text,Object id) {
		projectItems.put(text, id);
	}
	
	public synchronized static void updateStreamRepository(Object text,Object id) {
		streamItems.put(text, id);
	}
	
	
	/**
	 * Don't put synchronized here as this will be used by several method at the same time.
	 * Optimization constraint.
	 * 
	 * @param type
	 */
	public static void clearMap(int type) {
		switch(type) {
		case VIEW_REPOSITORY:
			 viewDirectory.clear();
			break;
		case ACTIVITY_REPOSITORY:
			viewActivity.clear();
			break;
		case OBSOLETE_REPOSITORY:
			obsoleteItems.clear();
			break;
		case PROJECT_REPOSITORY:
			projectItems.clear();
			break;
		case STREAM_REPOSITORY:
			streamItems.clear();
			break;
		default:
			
		}
	}
	
	public static Collection getCollection(int repository,int type) {
		Map repositoryType = null;
		Collection returnType = null;
		switch(repository) {
		case VIEW_REPOSITORY:
			repositoryType = viewDirectory;
			break;
		case ACTIVITY_REPOSITORY:
			repositoryType = viewActivity;
			break;
		case OBSOLETE_REPOSITORY:
			repositoryType = obsoleteItems;
			break;
		case PROJECT_REPOSITORY:
			repositoryType = projectItems;
			break;
		case STREAM_REPOSITORY:
			repositoryType = streamItems;
			break;
			
		default:			
		}
		
		if(repositoryType!=null) {
			if(type==0) {
			  	returnType = repositoryType.keySet();
			}else {
				returnType = repositoryType.values();
			}
		}
		return returnType;
	}
	
	public static Object getValueFromRepository(int tableType,Object key) {
		Map repositoryType = null;
		Object value = null;
		switch(tableType) {
		case VIEW_REPOSITORY:
			repositoryType = viewDirectory;
			break;
		case ACTIVITY_REPOSITORY:
			repositoryType = viewActivity;
			break;
		case OBSOLETE_REPOSITORY:
			repositoryType = obsoleteItems;
			break;
		case PROJECT_REPOSITORY:
			repositoryType = projectItems;
			break;
		case STREAM_REPOSITORY:
			repositoryType = streamItems;
			break;
		}
		
		if(repositoryType!=null) {
			value = repositoryType.get(key);
		}
		return value;
	}
	
	public static Object getActivityRelatedViewDir(Object activity) {
		Object viewKey = viewActivity.get(activity);
		return viewDirectory.get(viewKey);
	}
	

}
